package com.sanpu.kxe.business.repay.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.view.BankCardDetailsActivity;
import com.sanpu.kxe.business.repay.contract.UpdateCreditContract;
import com.sanpu.kxe.business.repay.model.RepayModel;
import com.sanpu.kxe.business.repay.view.UpdateCreditActivity;
import com.sanpu.kxe.jump.eventbus.RefreshBankEvent;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import org.greenrobot.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：修改信用卡信息
 * Created by 薛勇军 on 2019/4/10.
 */

public class UpdateCreditPresenter implements UpdateCreditContract.Presenter {

    private UpdateCreditContract.View mView;

    private CompositeSubscription mSubscription;

    private BankInfoOutput mInfoOutput;

    public UpdateCreditPresenter(UpdateCreditContract.View mView, BankInfoOutput infoOutput) {
        this.mView = mView;
        this.mInfoOutput = infoOutput;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void onCommit(String rsdTel, String billDay, String dueDay) {
        if (TextUtils.isEmpty(rsdTel)) {
            ToastUtil.showTextViewPrompt("预留手机号不能为空！");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(rsdTel)) {
            ToastUtil.showTextViewPrompt("预留手机号码不正确！");
            return;
        }
        if (TextUtils.isEmpty(billDay)) {
            ToastUtil.showTextViewPrompt("请选择账单日");
            return;
        }
        if (TextUtils.isEmpty(dueDay)) {
            ToastUtil.showTextViewPrompt("请选择还款日");
            return;
        }
        if (TextUtils.equals(billDay, dueDay)) {
            ToastUtil.showTextViewPrompt("信用卡账单日与还款日不能设为同一天");
            return;
        }
        if (mInfoOutput == null) {
            ToastUtil.showTextViewPrompt("银行卡信息有误，请返回重新选择！");
            return;
        }
        mView.showLoadingDialog(true);
        Subscription subscription = RepayModel.getInstance()
                .updateCreditBank(mInfoOutput.getMerchantCode(), mInfoOutput.getBankNo(),
                        rsdTel, billDay.substring(2, billDay.length()-1), dueDay.substring(2, dueDay.length()-1))
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoadingDialog();
                        EventBus.getDefault().post(new RefreshBankEvent());
                        AppContextUtil.finishActivity(UpdateCreditActivity.class.getName(),
                                BankCardDetailsActivity.class.getName());
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
